<?php

class GoodsModel extends Model {

	/*
	 *	查询所有商品信息
	 *	`open`               enum('Y','N') not null default 'Y' comment '是否开放（保留），枚举型（Y,N）',
   	 *  `display`            enum('Y','N') not null default 'Y' comment '是否展示（保留）',
   	 *  `enable`             enum('Y','N') not null default 'Y' comment '是否可用',
	 */
	public function selectAllByPage($ary, $limited = 20,$order=' id desc ') {
		if (is_string ( $ary )) {
			if ($ary)
				$ary = $ary . "  and is_delete='N' ";
		} else {
			$ary ['is_delete'] = 'N';
		}
		
		//$pageGoods = $this -> where ( $ary )->order ( ' id desc ' )->findPage ( $limited );
		$pageGoods = $this->kPageQuery($ary, $order ,$limited);
		return $pageGoods;
	}
	
	public function selectGoodsInfo($map,$field='*',$order=null,$limit=null,$ifpage=false){
		if(!empty($map['pid'])){
			if(is_array($map['pid'])){
				$ary ['partner_id'] = array('in',$map['pid']);
			}else{
				$ary ['partner_id'] = $map['pid'];
			}
		}
		if(!empty($map['gid'])){
			if(is_array($map['gid'])){
				$ary ['id'] = array('in',$map['gid']);
			}else{
				$ary ['id'] = $map['gid'];
			}
		}
		if(!empty($map['gsort']))$ary ['sort_id'] = $map['gsort'];
		if(!empty($map['get_type']))$ary ['get_type'] = $map['get_type'];
		$ary ['is_delete'] = 'N';
		//$ids = $this->where( $ary )->field($field)->findAll();
		if(!$ifpage){
			$data = $this->where( $ary )->field($field)->order($order)->limit($limit)->findAll();
		}else{
			$data = $this->kPageQuery($ary,$order,$limit,$field);	
		}
		Log::write('GoodsModel.selectGoodsInfo()sql='.($this->getLastSql()),Log::DEBUG,Log::FILE);
		return $data;
	}
	
	/**
	 * 判断商品是否为对应商户的商品。
	 * 商品均记录为总店，如果$pid为分店，则判断是否为对应总店的分店。
	 * @param String $gid
	 * @param String $pid
	 */
	public function isGoodsBelongPartner($gid,$pid){
		Log::write('GoodsModel.isGoodsBelongPartner('.$gid.','.$pid.')',Log::DEBUG,Log::FILE);
		
		$goods = $this->selectGoodsInfo(array('gid'=>$gid),' id , partner_id ',null,1);
		$goods = $goods[0];
		Log::write('GoodsModel.isGoodsBelongPartner('.$gid.','.$pid.')查询数据：gid->'.parseAry2Str($goods),Log::DEBUG,Log::FILE);
		
		if(empty($goods))return false;
		if($pid==$goods['partner_id'])return true;
		$result = D('Partner','Admin')->isBelongPartner($pid,$goods['partner_id']);
		
		Log::write('GoodsModel.isGoodsBelongPartner('.$gid.','.$pid.')数据->'.$pid.' | '.$goods['partner_id'].' | '.$result,Log::DEBUG,Log::FILE);
		
		if($result)return true;
		return false;
	}
	
	public function deleteByIds($ids){
		$map['id']	= array('in', $ids);
		$ary['is_delete']	=	'Y';
		
		$no = $this->where($map)->save($ary);
		return $no;
	}
	
	/**
	 * 更新商品验证商品总数
	 * @param unknown_type $pid
	 * @param unknown_type $checkednumber
	 */
	public function refreshCheckedNo($gid,$checkednumber){
		$goods = $this->where('id='.$gid)->find();
		if(empty($goods))return false;
		$map['checked_number']=intval($goods['checked_number'])+intval($checkednumber);
		$this->where('id='.$gid)->save($map);
		return $map['checked_number'];
	}
}
?>